package de.medieninf.android.sensimg;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;

public class KeypadViewActivity extends Activity  {
	final static String TAG = "KeypadViewActivity";
	final static int UPDATE_IMAGE = 42;
	
	private SensorService mSensorService;
	private KeyboardState mKeyboardState;
	
	TouchEventButton mButtonKey1;
	TouchEventButton mButtonKey2;
	TouchEventButton mButtonKey3;
	TouchEventButton mButtonKey4;
	TouchEventButton mButtonKey5;
	TouchEventButton mButtonKey6;
	TouchEventButton mButtonKey7;
	TouchEventButton mButtonKey8;
	TouchEventButton mButtonKey9;
	TouchEventButton mButtonKeyStar;
	TouchEventButton mButtonKey0;
	TouchEventButton mButtonKeyHash;
	TouchEventButton[] mButtons;
	
	
    private ServiceConnection mConnection = new ServiceConnection() {
    	@Override
        public void onServiceConnected(ComponentName className, IBinder service) {
    		Log.v(TAG, "trying to bind SensorService");
            mSensorService = ((SensorService.LocalBinder) service).getService();
            mKeyboardState = mSensorService.getKeyboardState();
            for (TouchEventButton teb : mButtons) {
            	teb.setKeyboardState(mKeyboardState);
            }
        }
		@Override
		public void onServiceDisconnected(ComponentName name) {
    		Log.v(TAG, "SensorService is disconnected");
			mSensorService = null;
		}
    };
	
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (mKeyboardState != null)
    		mKeyboardState.onKeyDown(keyCode, event);
		// Log.v(TAG, "String :" + keyboardState.getKeys());
    	return super.onKeyDown(keyCode, event);
	}

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
    	if (mKeyboardState != null)
    		mKeyboardState.onKeyUp(keyCode, event);
    	// Log.v(TAG, "String :" + keyboardState.getKeys());
    	return super.onKeyUp(keyCode, event);
	}		
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.keypad);
		mButtonKey1 = (TouchEventButton) findViewById(R.id.keypad_1);
		mButtonKey2 = (TouchEventButton) findViewById(R.id.keypad_2);
		mButtonKey3 = (TouchEventButton) findViewById(R.id.keypad_3);
		mButtonKey4 = (TouchEventButton) findViewById(R.id.keypad_4);
		mButtonKey5 = (TouchEventButton) findViewById(R.id.keypad_5);
		mButtonKey6 = (TouchEventButton) findViewById(R.id.keypad_6);
		mButtonKey7 = (TouchEventButton) findViewById(R.id.keypad_7);
		mButtonKey8 = (TouchEventButton) findViewById(R.id.keypad_8);
		mButtonKey9 = (TouchEventButton) findViewById(R.id.keypad_9);
		mButtonKeyStar = (TouchEventButton) findViewById(R.id.keypad_star);
		mButtonKey0 = (TouchEventButton) findViewById(R.id.keypad_0);
		mButtonKeyHash = (TouchEventButton) findViewById(R.id.keypad_hash);
		TouchEventButton[] buttons = {
			mButtonKey1,    mButtonKey2, mButtonKey3,
			mButtonKey4,    mButtonKey5, mButtonKey6,
			mButtonKey7,    mButtonKey8, mButtonKey9,
			mButtonKeyStar, mButtonKey0, mButtonKeyHash,
		};
		mButtons = buttons;
        bindService(
        		new Intent(this, SensorService.class),
                mConnection,
                Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onDestroy() {
		unbindService(mConnection);
		super.onDestroy();
	}
	
	
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    	super.onConfigurationChanged(newConfig);
    	// don't do anything, make sure that the activity is not restarted
    }
	
}
